package com.yingli.camera_system.camera;

import android.Manifest;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.widget.Button;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.yingli.camera_system.PermissionsUtils;
import com.yingli.camera_system.R;

import java.io.File;

public class Camera_Activity extends AppCompatActivity {
    //开始拍照的按钮
    private Button startCapture ;

    //--视图View
    private ImageView ivPreView;

    private static final int CAPTURE_RESULT = 1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);
        startCapture = findViewById(R.id.btn_start_capture);
        ivPreView = findViewById(R.id.iv_preview_pic);
        //动态判断权限是否存在，如果没有存在，动态申请
        startCapture.setOnClickListener(v -> PermissionsUtils.getInstance().checkPermissions(Camera_Activity.this,new String[]{Manifest.permission.CAMERA,Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE},new PermissionsUtils.IPermissionsResult() {

            @Override
            public void passPermissons() {
                launchCamera();
            }

            @Override
            public void forbitPermissons() {

            }
        }));
    }
    private Uri cameraUri;
    //启动系统相机 并携带拍照结果返回
    private void launchCamera() {
        //调用系统相机拍照后，所返回的图片分辨率比较低，所以需要把拍照后的图片保存到本地，并且读取
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        String cameraPath = Environment.getExternalStorageDirectory()+"/DCIM/Camera/"+System.currentTimeMillis()+".jpg";
        File mOutImage = new File(cameraPath);
        if (Build.VERSION.SDK_INT<Build.VERSION_CODES.N){
            cameraUri = Uri.fromFile(mOutImage);
        }else{
            cameraUri = FileProvider.getUriForFile(this,"com.tsy.media.fileprovider",mOutImage);
        }
        //设置图片输出（存储路径）
        intent.putExtra(MediaStore.EXTRA_OUTPUT,cameraUri);
        startActivityForResult(intent,CAPTURE_RESULT);
    }

    //跳转到其他界面后，其他界面携带数据返回当前页面，需要接收数据的回调方法
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case CAPTURE_RESULT:
                if (data != null && data.hasExtra("data")){
                    Bitmap bitmap = data.getParcelableExtra("data");
                    ivPreView.setImageBitmap(bitmap);
                } else{
                    if (cameraUri != null){
                        Glide.with(this)
                                .asBitmap()
                                .load(cameraUri)
                                .diskCacheStrategy(DiskCacheStrategy.NONE)
                                .into(ivPreView);
                    }
                }
                break;
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionsUtils.getInstance().onRequestPermissionsResult(this,requestCode,permissions,grantResults);
    }
}
